import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    count: 0,
    userList: [
      { id: 1, name: '张三', email: 'zhangsan@example.com' },
      { id: 2, name: '李四', email: 'lisi@example.com' },
      { id: 3, name: '王五', email: 'wangwu@example.com' }
    ]
  },
  mutations: {
    incrementCount(state) {
      state.count++
    },
    setUserList(state, users) {
      state.userList = users
    },
    removeUser(state, userId) {
      state.userList = state.userList.filter(user => user.id !== userId)
    }
  },
  actions: {
    async fetchUsers({ commit }) {
      // 模拟 API 调用
      await new Promise(resolve => setTimeout(resolve, 500))
      const users = [
        { id: 1, name: '张三', email: 'zhangsan@example.com' },
        { id: 2, name: '李四', email: 'lisi@example.com' },
        { id: 3, name: '王五', email: 'wangwu@example.com' },
        { id: 4, name: '赵六', email: 'zhaoliu@example.com' }
      ]
      commit('setUserList', users)
    },
    async deleteUser({ commit }, userId) {
      // 模拟 API 调用
      await new Promise(resolve => setTimeout(resolve, 300))
      commit('removeUser', userId)
    }
  }
})
